from text_dialog import TextDialog
from image_worker import ImageWorker
from mail_dialog import MailDialog

class ShareMenu:
    def __init__(self, editor, widgets):
        self.editor = editor
        
        # Fetch widgets.
        self.picasa_button = widgets.get_widget('Share:PicasaButton')
        self.mail_button = widgets.get_widget('Share:MailButton')
        self.back_button = widgets.get_widget('Share:BackButton')
        
        # Attach callbacks to buttons.
        self.picasa_button.connect('clicked', self.picasa_button_clicked)
        self.mail_button.connect('clicked', self.mail_button_clicked)
        self.back_button.connect('clicked', self.back_button_clicked)
        
    def picasa_button_clicked(self, widget):
        # Append the upload operation to the list.
        picasa_operation = None
        try:
            username = self.editor.program.config.get('google', 'username')
            password = self.editor.program.config.get('google','password')
            album = self.editor.program.config.get('google','album')
            picasa_operation = ('picasa', (username, password, album, self.editor.program.image_browser.current_image()))
        except:
            TextDialog("Error", "Google settings are invalid. Go to the settings and enter a valid Google account.").do_it()
            return
        
        # Start the image worker so that the file may be uploaded.
        worker = ImageWorker(self.editor.original_image, self.editor.pending_operations + [picasa_operation],
                             self.editor.program.task_monitor)
        worker.start()

        # Go back to the main menu.
        self.editor.share_vbox.hide()
        self.editor.main_vbox.show()

    def mail_button_clicked(self, widget):
        # Check that mail has been properly configured.
        try:
            server = self.editor.program.config.get('mail', 'smtp_server')
            port = self.editor.program.config.get('mail', 'smtp_port')
            sender = self.editor.program.config.get('mail', 'email_address')
        except:
            TextDialog("Error", "Mail settings are invalid. Go to the settings and enter your mail information.").do_it()
            return
        
        username = ''
        password = ''
        try:
            username = self.editor.program.config.get('mail', 'smtp_username')
            password = self.editor.program.config.get('mail', 'smtp_password')
        except:
            pass
        
        # Pop up a dialog that asks for the recipient and subject.
        dialog = MailDialog('Enter recipient')
        result = dialog.do_it()
        if result == None:
            return
        elif result[0] == '' or result[1] == '':
            TextDialog("Error", "Invalid recipient or subject given. Blank subject is not allowed.").do_it()
            return
        recipient = result[0]
        subject = result[1]
        
        # Perform the operation(s).
        # todo: The mime type should not be hardcoded.
        mail_operation = ('mail', (server, port, sender, recipient, subject, 'Image sent from AugIM.', username, password))
        worker = ImageWorker(self.editor.original_image, self.editor.pending_operations + [mail_operation],
                             self.editor.program.task_monitor)
        worker.start()
        
        # Go back to the main menu.
        self.editor.share_vbox.hide()
        self.editor.main_vbox.show()

                
    def back_button_clicked(self, widget):
        self.editor.share_vbox.hide()
        self.editor.main_vbox.show()